package com.etonedu.meals.http;

import com.blankj.utilcode.util.LogUtils;
import com.etonedu.meals.common.SPConst;
import com.etonedu.meals.enumeration.APICodeEnum;
import com.etonedu.meals.utils.MD5Utils;
import com.etonedu.meals.utils.SharedPreferencesUtil;
import com.etonedu.meals.utils.Utils;
import com.etonedu.meals.utils.XXTEAV2;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.xutils.x;

import java.util.HashMap;

public class ParamsUtils {

    public static String getURLParams(String type, String apiCode) {
        try {
            long time = System.currentTimeMillis();
            String timeStr = Utils.timestamp2String1(time, "yyyyMMddHHmmss");
            Gson gson = new Gson();

            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("code", apiCode);
            dataMap.put("type", type);

            String dataStr = gson.toJson(dataMap);
            String d = getEncryptToBase64String(dataStr);

            HashMap<String, Object> hashMap = new HashMap<>();
            String cliCode = getCliCode();
            hashMap.put("cliCode", cliCode);

            hashMap.put("data", d);
            hashMap.put("time", timeStr);
            hashMap.put("sign", MD5Utils.t(d, timeStr));

            LogUtils.d("params = " + gson.toJson(hashMap));

            return gson.toJson(hashMap);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getHeader(String apiCode, String content) {
        try {
            HashMap<String, Object> headerMap = new HashMap<>();
            String devId = Utils.getDevCode();
            headerMap.put("cid", devId);
            headerMap.put("cli", 8);
            headerMap.put("time", Utils.timestamp2String1(System.currentTimeMillis(), "yyyyMMddHHmmss"));
            if (apiCode.equals(APICodeEnum.agw10111006.getApiCode())) {

                String cliCode = getCliCode();
                headerMap.put("token",cliCode);
            } else {
                headerMap.put("token", content);
            }
            headerMap.put("ver", APICodeEnum.getverByCode(apiCode));

            Gson gson = new Gson();
            String headerStr = gson.toJson(headerMap);
            String header = getEncryptToBase64String(headerStr);
            LogUtils.e("apiCode = " + apiCode + ",header = " + header);

            return header;
        } catch (Exception e) {
            return "";
        }
    }

    public static String getTokenParams() {
        try {
            HashMap<String, Object> dataMap = new HashMap<>();
//            String ip = Utils.getLocalIpAddress(x.app());
            SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
            String ip = spu.getString(SPConst.IP_ADDR);
            if (Utils.isEmpty(ip)) {
                ip = Utils.getLocalIpAddress(x.app());
                if ("0.0.0.0".equals(ip)) {
                    ip = Utils.getLocalIp();
                }
                spu.setString(SPConst.IP_ADDR,ip);
            }
            String time = Utils.timestamp2String1(System.currentTimeMillis(), "yyyyMMddHHmmss");
            LogUtils.e("ip = " + ip);
            String cid = Utils.getDevCode();
            dataMap.put("ip", ip);
            dataMap.put("cid", cid);
            dataMap.put("timestamp", time);
            dataMap.put("hashcode", MD5Utils.getParams(time, ip, cid));

            Gson gson = new Gson();
            String params = gson.toJson(dataMap);

            return params;
        } catch (Exception e) {
            return "";
        }
    }

    public static String getParams(String apiCode, String data) {
        try {
            long time = System.currentTimeMillis();
            String timeStr = Utils.timestamp2String1(time, "yyyyMMddHHmmss");
            Gson gson = new GsonBuilder().disableHtmlEscaping().create();


            HashMap<String, Object> dataMap = new HashMap<>();

            String cliCode = getCliCode();
            dataMap.put("cliCode", cliCode);

            dataMap.put("data", data);
            dataMap.put("time", timeStr);
            dataMap.put("sign", MD5Utils.t(data, timeStr));

//            LogUtils.d("params = "+gson.toJson(dataMap));
            LogUtils.d("apiCode = " + apiCode + ",params = " + gson.toJson(dataMap));
            return gson.toJson(dataMap);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getEncrypt(String apiCode, String params) {
        try {
            String result = getEncryptToBase64String(params);

            LogUtils.d("apiCode = " + apiCode + ",params = " + params);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getDecrypt(String apiCode, String params) {
        try {
            String result = getDecryptBase64StringToString(params);

            LogUtils.d("apiCode = " + apiCode + ",params = " + params);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static String getEncryptToBase64String(String params) {
        String result = "";
        if ("http://agw.52ess.cn/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("测试");
            result = XXTEAV2.encryptToBase64String(params, MD5Utils.ENCRY_DECRYPT_KEY);
        } else if ("http://agw.woniukid.cn/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("生产");
            result = XXTEAV2.encryptToBase64String(params, MD5Utils.PROD_ENCRY_DECRYPT_KEY);
        } else if ("http://47.106.165.234:8181/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("测试");
            result = XXTEAV2.encryptToBase64String(params, MD5Utils.ENCRY_DECRYPT_KEY);
        } else if ("http://123.207.103.39:8080/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("生产");
            result = XXTEAV2.encryptToBase64String(params, MD5Utils.PROD_ENCRY_DECRYPT_KEY);
        }
        return result;
    }

    public static String getDecryptBase64StringToString(String params) {
        String result = "";

        if ("http://agw.52ess.cn/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("测试");
            result = XXTEAV2.decryptBase64StringToString(params, MD5Utils.ENCRY_DECRYPT_KEY);
        } else if ("http://agw.woniukid.cn/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("生产");
            result = XXTEAV2.decryptBase64StringToString(params, MD5Utils.PROD_ENCRY_DECRYPT_KEY);
        } else if ("http://47.106.165.234:8181/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("测试");
            result = XXTEAV2.decryptBase64StringToString(params, MD5Utils.ENCRY_DECRYPT_KEY);
        } else if ("http://123.207.103.39:8080/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("生产");
            result = XXTEAV2.decryptBase64StringToString(params, MD5Utils.PROD_ENCRY_DECRYPT_KEY);
        }
        return result;
    }

    private static String getCliCode() {
        String cliCode = "";
        if ("http://agw.52ess.cn/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("测试");
            cliCode = MD5Utils.CLI_CODE;
        } else if ("http://agw.woniukid.cn/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("生产");
            cliCode = MD5Utils.PROD_CLI_CODE;
        } else if ("http://47.106.165.234:8181/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("测试");
            cliCode = MD5Utils.CLI_CODE;
        } else if ("http://123.207.103.39:8080/api".equals(HttpUrl.DOMAIN)) {
            LogUtils.e("生产");
            cliCode = MD5Utils.PROD_CLI_CODE;
        }
        return cliCode;
    }
}

